package de.ergin.fuhrparkverwaltung.activities;

import de.ergin.fuhrparkverwaltung.R;
import de.ergin.fuhrparkverwaltung.daten.DatenbankManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class FahrzeugDetails extends Activity {

	protected DatenbankManager mHelper;
	protected SQLiteDatabase mDatenbank;
	protected Cursor mCarCursor;
	protected TextView mFahrzeugTextView;
	protected int mCarID;
	

	@Override
	protected void onResume() {
		super.onResume();
		mDatenbank = mHelper.getReadableDatabase();
		showCarInformationOnActivityScreen();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mDatenbank.close();
	}

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fahrzeugdetails);

		mCarID = getIntent().getIntExtra("CarID", 0);
		mHelper = new DatenbankManager(this);
		mDatenbank = mHelper.getReadableDatabase();

		showCarInformationOnActivityScreen();
	}

	public void onButtonClick(View view) {
		switch (view.getId()) {

		case R.id.btn_Service:
			Intent intent = new Intent(this, FahrzeugInspektion.class);
			intent.putExtra("carID", mCarID);
			startActivity(intent);
			break;
		case R.id.btn_Tuev:
			Intent intent3 = new Intent(this, FahrzeugTuev.class);
			intent3.putExtra("carID", mCarID);
			startActivity(intent3);
			break;

		case R.id.btn_LastService:
			Intent intent2 = new Intent(this, FahrzeugDiagramm.class);
			intent2.putExtra("carID", mCarID);
			startActivity(intent2);
			break;
		}
	}

	protected void ShowDeleteDialog() {

		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle(getString(R.string.str_wollen_sie_das_fahrzeug_wirklich_loeschen));
		dialog.setPositiveButton(getString(R.string.str_ja),GetOnOkClickListener());
		dialog.setNegativeButton(getString(R.string.str_nein),GetOnNoClickListener());
		dialog.show();
	}

	private OnClickListener GetOnOkClickListener() {
		OnClickListener result = new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				FahrzeugDetails.this.onOkClick(dialog, which);
			}
		};
		return result;
	}

	private void onOkClick(DialogInterface dialog, int whichButton) {
		try {

			mDatenbank.execSQL(DatenbankManager.FAHRZEUG_DELETE, new String[] {""+mCarID });

		} catch (Exception e) {
			throw new NullPointerException(e.toString());
		}
		
		startActivity(new Intent(FahrzeugDetails.this, FahrzeugAuswaehlen.class));
		finish();
	}

	private OnClickListener GetOnNoClickListener() {
		OnClickListener result = new OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				return;
			}
		};

		return result;
	}
	
	private void showCarInformationOnActivityScreen() {
		mFahrzeugTextView = (TextView) findViewById(R.id.txt_SelectCar);
		mCarCursor = mDatenbank.rawQuery(DatenbankManager.FAHRZEUGE_SELCET_WARTUNG, new String[]{""+mCarID});
		if (mCarCursor.getCount() == 0) {
			mFahrzeugTextView.setText(R.string.str_noch_keine_wartung_vorhanden);
		} else {
			mCarCursor.moveToFirst();
			mFahrzeugTextView.setText(
					  getString(R.string.str_die_id_des_fahrzeugs_ist)
					+" "
					+ mCarCursor.getInt(mCarCursor.getColumnIndex("id"))
					+"\n"
					+ getString(R.string.str_letzte_wartung_war_am)
					+" "
					+ mCarCursor.getString(mCarCursor.getColumnIndex("datum"))
					+"\n"
					+ getString(R.string.str_aktuelle_laufkilometer)
					+" "
					+ mCarCursor.getInt(mCarCursor.getColumnIndex("laufkilometer"))
					+" "
					+ getString(R.string.str_km)
					);
		}
	}
}
